//
//  NewFeatureViewController.swift
//  波波微博
//
//  Created by wave on 15/7/31.
//  Copyright © 2015年 wave. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {

    //图片总数
    let imageCount = 4
    
    //布局界面
    private let layout = BBBFlowLayout()
    
    //因为这里UICollectionView指定的构造函数是带layout的,所以不需要添加override
    init(){
        super.init(collectionViewLayout: layout)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        //禁止使用sb开发,用这个风险小,考虑因素少             .  用super.init...考虑因素多,而且都需要测试
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.redColor()
        
        //这句话的意思是注册类,
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }


    
    //MARK: - 数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return imageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.imageIndex = indexPath.item
        return cell
    }
    
    //collectionView的代理方法
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        //获取当前显示的indexPath
        let path = collectionView.indexPathsForVisibleItems().last!
        
        //判断是否是末尾的indexPath  --> 这里需要注意,图片的索引要-1才能是path.item的索引值
        if path.item == imageCount - 1{
        
            //因为动画按钮在下面的类中,所以需要用cell才可以调用播放动画的函数
            let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
            cell.startButtonAnim()
        }
    }
    
}

class NewFeatureCell:UICollectionViewCell{

    //图片索引,设置图片
    private var imageIndex : Int = 0 {
        
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
    }
    
    //MARK: - 设置UI
    //重写init
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    //写一个视图类,供调用
    private func prepareUI(){
    
        contentView.addSubview(iconView)
        contentView.addSubview(startButton)
        
        //设置iconView的自动布局
        iconView.translatesAutoresizingMaskIntoConstraints = false
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views:["subview":iconView]))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views:["subview":iconView]))
        
        //给按钮设置自动布局
        //开始按钮
        startButton.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil,offset:CGPoint(x: 0, y: -160))
    }
    
    
    //开始按钮的动画
    private func startButtonAnim(){
        
        startButton.hidden = false
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        //禁用按钮点击
        startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            //默认恢复形变
            //这个属性默认恢复到原来的大小
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
                //恢复页面点击
                self.startButton.userInteractionEnabled = true
        }
    }
    
    
    //懒加载控件
    private lazy var iconView = UIImageView()
    
    private lazy var startButton:UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        
        button.sizeToFit()
        
        button.addTarget(self, action: "clickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    //点击button执行的函数
    func clickStartButton(){
        
        //发送通知
        print("点我啦点我啦")
        NSNotificationCenter.defaultCenter().postNotificationName(BBBSwitchMainInterfaceNotification, object: true)
    }
    
}


//自定义流水步距
private class BBBFlowLayout:UICollectionViewFlowLayout{
    
    private override func prepareLayout() {
    
        //如果还没有设置layout,获取数量之后,准备cell之前,会被调用一次
        //准备布局属性
        itemSize = collectionView!.bounds.size

        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
}














